// The view where you choose in which area you want to do exercises.
//Tells the exercisecontroller to start the exercise accordning to what area the user has choosen
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SelectAreaView.java
 *
 * Created on 2011-maj-10, 14:24:47
 */
package edu.chalmers.se.tda367.gui.view;

import edu.chalmers.se.tda367.controller.ExerciseController;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import edu.chalmers.se.tda367.main.Pluggis;
import edu.chalmers.se.tda367.question.MathQuestionConFig;

/**
 *
 * @author Hanna
 */
public class SelectAreaView extends javax.swing.JPanel implements ActionListener, PropertyChangeListener {

    private PropertyChangeSupport pcs;
    private ExerciseController exCon = new ExerciseController();

    /** Creates new form SelectAreaView */
    public SelectAreaView() {
        pcs = new PropertyChangeSupport(this);
        initComponents();
        additionButton.addActionListener(this);
        subtractionButton.addActionListener(this);
        multiplicationButton.addActionListener(this);
        divisionButton.addActionListener(this);
        previousButton.addActionListener(this);

        if (Pluggis.getInstance().getSelectedUser().getLevel() == 1) {
            multiplicationButton.setEnabled(false);
            divisionButton.setEnabled(false);
        }
    }

    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        pcs.addPropertyChangeListener(listener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        additionButton = new javax.swing.JButton();
        subtractionButton = new javax.swing.JButton();
        multiplicationButton = new javax.swing.JButton();
        divisionButton = new javax.swing.JButton();
        previousButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(edu.chalmers.se.tda367.main.Main.class).getContext().getResourceMap(SelectAreaView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setMaximumSize(new java.awt.Dimension(820, 550));
        setMinimumSize(new java.awt.Dimension(820, 550));
        setName("Form"); // NOI18N

        additionButton.setBackground(resourceMap.getColor("additionButton.background")); // NOI18N
        additionButton.setIcon(resourceMap.getIcon("additionButton.icon")); // NOI18N
        additionButton.setText(resourceMap.getString("additionButton.text")); // NOI18N
        additionButton.setName("additionButton"); // NOI18N
        additionButton.setOpaque(false);

        subtractionButton.setIcon(resourceMap.getIcon("subtractionButton.icon")); // NOI18N
        subtractionButton.setText(resourceMap.getString("subtractionButton.text")); // NOI18N
        subtractionButton.setName("subtractionButton"); // NOI18N
        subtractionButton.setOpaque(false);

        multiplicationButton.setIcon(resourceMap.getIcon("multiplicationButton.icon")); // NOI18N
        multiplicationButton.setText(resourceMap.getString("multiplicationButton.text")); // NOI18N
        multiplicationButton.setName("multiplicationButton"); // NOI18N
        multiplicationButton.setOpaque(false);

        divisionButton.setIcon(resourceMap.getIcon("divisionButton.icon")); // NOI18N
        divisionButton.setText(resourceMap.getString("divisionButton.text")); // NOI18N
        divisionButton.setName("divisionButton"); // NOI18N
        divisionButton.setOpaque(false);

        previousButton.setFont(resourceMap.getFont("previousButton.font")); // NOI18N
        previousButton.setText(resourceMap.getString("previousButton.text")); // NOI18N
        previousButton.setName("previousButton"); // NOI18N
        previousButton.setOpaque(false);

        jLabel1.setIcon(resourceMap.getIcon("jLabel1.icon")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 466, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 169, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(38, 38, 38))
            .addGroup(layout.createSequentialGroup()
                .addGap(55, 55, 55)
                .addComponent(additionButton, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(subtractionButton, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(multiplicationButton, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(divisionButton, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(80, 80, 80))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(155, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(divisionButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(multiplicationButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(subtractionButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(additionButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(previousButton, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void goBackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goBackButtonActionPerformed
        Pluggis.getInstance().getMainView().removeAll();
        Pluggis.getInstance().getMainView().add(new LoggedInView());
        Pluggis.repaintViews();
    }//GEN-LAST:event_goBackButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton additionButton;
    private javax.swing.JButton divisionButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton multiplicationButton;
    private javax.swing.JButton previousButton;
    private javax.swing.JButton subtractionButton;
    // End of variables declaration//GEN-END:variables

    public void actionPerformed(ActionEvent e) {

        MathQuestionConFig config = new MathQuestionConFig();
        Pluggis.getInstance().getMainView().removeAll();
        int level = Pluggis.getInstance().getSelectedUser().getLevel();

        if (e.getSource() == previousButton) {
            Pluggis.getInstance().getMainView().removeAll();
            Pluggis.getInstance().getMainView().add(new LoggedInView());
            Pluggis.repaintViews();
        } else if (e.getSource() == additionButton) {
            config.setAddQuestion(level);
        } //   typeOfQuestion = "addition";
        else if (e.getSource() == subtractionButton) {
            config.setSubQuestion(level);
        } else if (e.getSource() == multiplicationButton) {
            config.setMulQuestion(level);
            //typeOfQuestion = "multiplication";
        } else if (e.getSource() == divisionButton) {
            config.setDivQuestion(level);
            // typeOfQuestion = "division";
        }

        exCon.startExercise(config);

        Pluggis.getInstance().getMainView().add(exCon.getView());
        Pluggis.getInstance().getMainView().repaint();
        Pluggis.getInstance().getMainView().validate();

    }

    public void propertyChange(PropertyChangeEvent evt) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}
